# ===----------------------------------------------------------------------=== #
# Copyright (c) 2024, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #

name: Test Examples

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
  workflow_dispatch:

permissions:
  contents: read
  pull-requests: read

jobs:
  test-examples:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    env:
      DEBIAN_FRONTEND: noninteractive
      LLVM_VERSION: 17

    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: Download Modular installer
        run: |
          curl -s https://get.modular.com | sh -

      - name: Install stable Mojo compiler
        run: |
          # The <auth_token> of "examples" is arbitrary but something
          # needs to be provided.
          modular auth examples
          modular install mojo

      - name: Install build tools
        run: |
          wget https://apt.llvm.org/llvm.sh
          chmod +x llvm.sh
          sudo ./llvm.sh ${{env.LLVM_VERSION}}

          # Make common LLVM binaries (including FileCheck) in our PATH so they work when used in an unversioned context
          # For example, instead of saying `FileCheck-17` which exists in `/usr/bin`, this allows us to just call
          # FileCheck unqualified.
          sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-${{env.LLVM_VERSION}} 100
          sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-${{env.LLVM_VERSION}} 100
          sudo update-alternatives --install /usr/bin/lld lld /usr/bin/lld-${{env.LLVM_VERSION}} 100
          sudo update-alternatives --install /usr/bin/ld.lld ld.lld /usr/bin/ld.lld-${{env.LLVM_VERSION}} 100
          sudo update-alternatives --install /usr/bin/lldb lldb /usr/bin/lldb-${{env.LLVM_VERSION}} 100
          sudo update-alternatives --install /usr/bin/FileCheck FileCheck /usr/bin/FileCheck-${{env.LLVM_VERSION}} 100


          python3 -m pip install lit

      - name: Run examples
        run: |
          export MODULAR_HOME="/home/runner/.modular"
          export PATH="/home/runner/.modular/pkg/packages.modular.com_mojo/bin:$PATH"
          ./examples/run-examples.sh
